#!/bin/bash

if [ $# -ne 1 ];then
    echo usage: $1 service
    exit 1
fi

service=$1
echo $service

goserv=`echo ${service:0:1} | awk '{print toupper($0)}'`${service:1}
echo $goserv

# create module for service
mkdir $service
cd $service
go mod init $service
cd -

# root dir for service
cp tmpl/.gitignore $service/.gitignore
sed "s/{service}/$service/g" tmpl/build.sh > $service/build.sh
sed "s/{service}/$service/g" tmpl/main.go.tmpl | sed "s/{Service}/$goserv/g" > $service/main.go

# cicd dir for service
mkdir -p $service/cicd
sed "s/{service}/$service/g" tmpl/cicd/Dockerfile.debug > $service/cicd/Dockerfile.debug
sed "s/{service}/$service/g" tmpl/cicd/Dockerfile.release > $service/cicd/Dockerfile.release

# etc dir for service
mkdir -p $service/etc
sed "s/{service}/$service/g" tmpl/etc/service.yaml > $service/etc/config-$service.yaml

# service dir for service
mkdir -p $service/$service
sed "s/{service}/$service/g" tmpl/service/api.go.tmpl | sed "s/{Service}/$goserv/g" > $service/$service/api.go
sed "s/{service}/$service/g" tmpl/service/config.go.tmpl | sed "s/{Service}/$goserv/g" > $service/$service/config.go
sed "s/{service}/$service/g" tmpl/service/service.go.tmpl | sed "s/{Service}/$goserv/g" > $service/$service/$service.go

cd $service
go mod tidy
